﻿using System;
using System.Collections.Generic;
using System.Linq;
using Infrastructure;
using Microsoft.EntityFrameworkCore;
using NUnit.Framework;
using OpenAuth.App.Interface;
using OpenAuth.App.Request;
using OpenAuth.App.Response;
using OpenAuth.Repository;
using OpenAuth.Repository.Domain;
using OpenAuth.Repository.Interface;


namespace OpenAuth.App
{
    public class InspectStandardApp : BaseApp<InspectStandard>
    {
        private RevelanceManagerApp _revelanceApp;
        private IAuth _auth;
        private OpenAuthDBContext _content;

        /// <summary>
        /// 加载列表
        /// </summary>
        public TableData Load(QueryInspectStandardListReq request)
        {
            var loginContext = _auth.GetCurrentUser();
            if (loginContext == null)
            {
                throw new CommonException("登录已过期", Define.INVALID_TOKEN);
            }

            var properties = loginContext.GetProperties("InspectStandard");

            if (properties == null || properties.Count == 0)
            {
                throw new Exception("当前登录用户没有访问该模块字段的权限，请联系管理员配置");
            }

            List<string> orgIdList = new List<string>();
            if (!string.IsNullOrEmpty(request.orgId))
            {
                var org = loginContext.Orgs.SingleOrDefault(u => u.Id == request.orgId);
                foreach (Org item in loginContext.Orgs)
                {
                    if (item.CascadeId.Contains(org.CascadeId))
                    {
                        orgIdList.Add(item.Id);
                    }
                }
            }
            else
            {
                orgIdList = loginContext.Orgs.Select(u => u.Id).ToList();
            }

            var result = new TableData();
            var objs = UnitWork.Find<InspectStandard>(u => orgIdList.Contains(u.OrgId));

            if (!string.IsNullOrEmpty(request.key))
            {
                objs = objs.Where(u => u.Name.Contains(request.key));
            }


            var propertyStr = string.Join(',', properties.Select(u => u.Key));
            result.columnHeaders = properties;
            result.data = objs.OrderByDescending(u => u.CreateTime)
                .Skip((request.page - 1) * request.limit)
                .Take(request.limit).Select($"new ({propertyStr})");
            result.count = objs.Count();
            return result;
        }

        public InspectStandard GetOne(String id)
        {
            return UnitWork.Find<InspectStandard>(u => u.Id == id).Include(t => t.Items).FirstOrDefault();
        }

        public void Add(InspectStandard obj)
        {
            //Repository.Add(obj);
            obj.CreateTime = DateTime.Now;
            UnitWork.Add(obj);
            UnitWork.Save();
        }

        public void Update(InspectStandard obj)
        {
            UnitWork.Delete<InspectStandardItems>(u => u.InspectStandardId == obj.Id);

            UnitWork.Update<InspectStandard>(u => u.Id == obj.Id, u => new InspectStandard
            {
                Name = obj.Name,
                OrgId = obj.OrgId,
                Remark = obj.Remark,
            });
            foreach (var item in obj.Items)
            {
                item.InspectStandardId = obj.Id;
                UnitWork.Add(item);
            }

            UnitWork.Save();
        }

        public void DeleteByIds(string[] ids)
        {
            UnitWork.Delete<InspectStandardItems>(u => ids.Contains(u.InspectStandardId));
            UnitWork.Delete<InspectStandard>(u => ids.Contains(u.Id));
            UnitWork.Save();
        }

        public InspectStandardApp(IUnitWork unitWork, IRepository<InspectStandard> repository,
            RevelanceManagerApp app, IAuth auth, OpenAuthDBContext openAuthDB) : base(unitWork, repository)
        {
            _revelanceApp = app;
            _auth = auth;
            //_content = openAuthDB;
        }
    }
}